package com.xh.bussiness.teachbook.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.teachbook.bean.dto.WrongQuestionsDto;
import com.xh.bussiness.teachbook.bean.request.WrongQuestionsRequest;
import com.xh.bussiness.teachbook.dao.WrongQuestionsMapper;
import com.xh.bussiness.teachbook.model.WrongQuestions;
import com.xh.bussiness.teachbook.service.IWrongQuestionsService;
import com.xh.bussiness.tiku.bean.dto.KnowledgeDto;
import com.xh.bussiness.tiku.model.Knowledge;
import com.xh.bussiness.tiku.model.Question;
import com.xh.bussiness.tiku.service.IKnowledgeService;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;

/**
 * <p>
 * 错题本 服务实现类
 * </p>
 *
 * @author JQM
 * @since 2019-08-27
 */
@Service
public class WrongQuestionsServiceImpl extends BaseServiceImpl<WrongQuestionsMapper, WrongQuestions> implements IWrongQuestionsService {
	
	
	private IQuestionService questionService;
	
	private IKnowledgeService knowledgeService;
	
    @Override
    public XhPage queryWrongQuestionsPage(XhPage page, WrongQuestionsRequest bean) {
        QueryWrapper qw=new QueryWrapper<WrongQuestions>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryWrongQuestions(WrongQuestionsRequest bean) {
        QueryWrapper qw=new QueryWrapper<WrongQuestions>();
        //拼装条件
        qw=prepareParams(bean, qw);
        if(!StringUtils.isEmpty(bean.getSubjectId())){
        	qw.eq("subject_id", bean.getSubjectId());
        }
        if(bean.getType() != null){
        	qw.eq("type", bean.getType());
        }
        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(WrongQuestionsRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }

	@Override
	public List<Map<String, Object>> queryCountBySubject(String stuId) {
		//TODO: 查询学科
		return this.dao.queryCountBySubject(stuId);
	}

	@Override
	public List<Question> queryBySubject(WrongQuestionsRequest bean) {
		QueryWrapper qw=new QueryWrapper<WrongQuestions>();
		if(!StringUtils.isEmpty(bean.getSubjectId())){
        	qw.eq("subject_id", bean.getSubjectId());
        }
        if(bean.getType() != null){
        	qw.eq("type", bean.getType());
        }
        List<WrongQuestions> wqList = this.dao.selectList(qw);
        if(CollectionUtils.isEmpty(wqList)){
        	return null;
        }
        Set<String> ids = new HashSet<String>();
        QueryWrapper qw_quest=new QueryWrapper<Question>();
        qw_quest.in("id", ids);
        List<Question> questions = questionService.queryByCondition(qw);
		return questions;
	}

	@Override
	public XhPage<WrongQuestionsDto> queryByPage(XhPage<WrongQuestionsDto> page, WrongQuestionsRequest bean) {
		
		List<WrongQuestionsDto> list = this.dao.queryByPage(page, bean);
		Long total = this.dao.queryCount(bean);
		page.setRecords(list);
		page.setTotal(total);
		return page;
	}

	@Override
	public List<KnowledgeDto> queryCountKnowledge(WrongQuestionsRequest bean) {
		return this.dao.queryCountKnowledge(bean.getStuId());
	}

	@Override
	public List<KnowledgeDto> queryCountByKnowledgeTree(WrongQuestionsRequest bean) {
		// 1、查出树形结构
		// 2、查出每个节点下的错题数量
		// 3、删除节点错题数量为零的节点
		QueryWrapper qw = new QueryWrapper();
		// 查出所有知识点
		List<KnowledgeDto> all = knowledgeService.selectList(qw, KnowledgeDto.class);
		// 查询出关联知识点的错题个数
		List<KnowledgeDto> list = this.dao.queryCountKnowledge(bean.getStuId());
		// 递归计算每个知识点下的错误数量
		countWrongQuestion(all, list);
		
		// 去掉错题数为零的节点
		List<KnowledgeDto> result = new ArrayList<KnowledgeDto>();
		for(KnowledgeDto k : all){
			if(k.getCount() != null && k.getCount() !=0){
				result.add(k);
			}
		}
		knowledgeService.findChilds(knowledgeService.getRoots(result), result);
		return result;
	}
	
	//TODO: 这个算法有待检验
	private void countWrongQuestion(List<KnowledgeDto> all, List<KnowledgeDto> list){
		List<KnowledgeDto> parents = new ArrayList<KnowledgeDto>();
		for(KnowledgeDto dto : list){
			for(KnowledgeDto k : all){
				if(dto.getParentId().equals(k.getId())){
					k.setCount(k.getCount() == null ? dto.getCount() : k.getCount()+dto.getCount());
					parents.add(k);
				}
			}
		}
		countWrongQuestion(all, parents);
	}
}
